package com.da.javatest.design.schema.singleton;

public class SingletonTest07 {
    private static volatile SingletonTest07 instance;

    //构造器私有化
    private SingletonTest07() {}

    //写一个静态内部类,该类中有一个静态属性 Singleton
    private static class SingletonInstance {
        private static final SingletonTest07 INSTANCE = new SingletonTest07();
    }

    //提供一个静态的公有方法，直接返回SingletonInstance.INSTANCE
    public static synchronized SingletonTest07 getInstance() {
        return SingletonInstance.INSTANCE;
    }

    public static void main(String[] args) {
        System.out.println("使用静态内部类完成单例模式");
        SingletonTest07 instance = SingletonTest07.getInstance();
        SingletonTest07 instance2 = SingletonTest07.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}
